import {getRequest} from "./api";


export const initMenus=(router,store)=>{
    if (store.state.routes.length>0){
        return;
    }
    getRequest("/system/config/menu").then(data=>{
        if(data){
            let fmRoute = {
                path:'/home',
                name:'个人中心',
                icocls:'',
                meta:'',
                children:[
                    {
                        path:'/userCenter',
                        name:'个人中心',
                        icocls:'',
                        meta:'',
                        children:null,
                        component(resolve){
                            require(['../views/sys/UserCenter.vue'],resolve)
                        }
                    }
                ],
                component(resolve){
                    require(['../views/Home.vue'],resolve)
                }
            };
            //console.log("初始"+data)
            let fmtRoutes = formatRoutes(data);
            fmtRoutes.push(fmRoute)
            router.addRoutes(fmtRoutes);
            store.commit('initRoutes',fmtRoutes);
            store.dispatch('connect');
        }
    })
};

export const formatRoutes=(routes)=>{
    let fmRoutes = [];
    routes.forEach(router=>{
        let {
            path,
            component,
            icocls,
            name,
            meta,
            children
        }=router;
        if (children && children instanceof Array){
            children = formatRoutes(children);
        }
        let fmRoute = {
            path:path,
            name:name,
            icocls:icocls,
            meta:meta,
            children:children,
            component(resolve){
                console.log(component);
                if(component.startsWith('Mem')){
                    require(['../views/mem/'+component+'.vue'],resolve)
                }else if(component.startsWith('Clock')){
                    require(['../views/clock/'+component+'.vue'],resolve)
                }else if(component.startsWith('Res')){
                    require(['../views/res0/'+component+'.vue'],resolve)
                }else if(component.startsWith('Sys')){
                    require(['../views/sys/'+component+'.vue'],resolve)
                }
                else {
                    require(['../views/'+component+'.vue'],resolve)
                }

            }
        };
        fmRoutes.push(fmRoute)
    });
    return fmRoutes;
};